/*
 * @Author: shenya shenya0203@163.com
 * @Date: 2024-01-03 14:33:10
 * @LastEditors: shenya shenya0203@163.com
 * @LastEditTime: 2024-04-16 13:01:50
 * @FilePath: /code/include/queue.h
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#ifndef __QUEUE_H__
#define __QUEUE_H__

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>

#define MAX_MESSAGE_LENGTH 1024
#define MAX_MESSAGE_PAYLOAD_LENGTH 1000 // 在前面取24字节，前两字节表示分包序号，后两字节表示分包总数。
#define MAX_MESSAGE_COUNT 12

typedef struct
{
    char data[MAX_MESSAGE_LENGTH];
    // char *data;
    int length;
} Message;

typedef struct
{
    uint16_t index;      // 分包序号
    uint16_t count;      // 分包总数
    uint16_t type;       // 类型
    int32_t payloadLen; // 总大小
} MessageIndex;

typedef struct
{
    Message messages[MAX_MESSAGE_COUNT];
    int front, rear;
    pthread_mutex_t mutex;
    pthread_cond_t cond;
} MessageQueue;

void initializeMessageQueue(MessageQueue *mq);
int isMessageQueueEmpty(MessageQueue *mq);
int isMessageQueueFull(MessageQueue *mq);
void enqueueMessage(MessageQueue *mq, const char *data, int length);
void enqueueMessageIndex(MessageQueue *mq, const char *data, int length, MessageIndex messIndex);
Message dequeueMessage(MessageQueue *mq);

#endif
